#include "qresetwidget.h"

#include "qproperty.h"

#include <QIcon>
#include <QHBoxLayout>
#include <QPainter>

QResetWidget::QResetWidget(QProperty *pro, QWidget *parent) :
    QWidget(parent),
    m_property(pro)
{
    m_resetButton=new QToolButton(this);
    m_resetButton->setIcon(QIcon(":/images/reset.png"));
    m_resetButton->setIconSize(QSize(8,8));
    m_resetButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::MinimumExpanding));

    connect(m_property,SIGNAL(changed()),this,SLOT(propertyChanged()));

    connect(m_resetButton,SIGNAL(clicked()),this,SLOT(reset()));
}

void QResetWidget::setWidget(QWidget *wid)
{
    QHBoxLayout *h=new QHBoxLayout;
    this->setLayout(h);
    h->setContentsMargins(3,0,1,0);
    h->setSpacing(0);
    h->addWidget(wid);
    h->addWidget(m_resetButton);
    setFocusProxy(wid);
}

void QResetWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.fillRect(this->rect(),Qt::white);
}

void QResetWidget::enabledButton(bool enable)
{
    m_resetButton->setEnabled(enable);
}

void QResetWidget::propertyChanged()
{
    enabledButton(m_property->modified());
}

void QResetWidget::reset()
{
    m_property->setValue(m_property->defaultValue());
    m_property->setModified(false);
    enabledButton(false);
}

void QResetWidget::setText(const QString text)
{
    m_text=text;
}

QString QResetWidget::text()
{
    return m_property->valueText();
}

void QResetWidget::setReset(bool b)
{
    m_resetButton->setVisible(b);
}
